package frontend.visitor.symboltbl;

import frontend.visitor.symboltbl.scope.RootScope;
import frontend.visitor.symboltbl.scope.Scope;

import java.util.Stack;

public class ScopeStackTrace {
    private final Stack<Scope> scopeStackContainer;

    public ScopeStackTrace(RootScope rootScope) {
        this.scopeStackContainer = new Stack<>();
        this.scopeStackContainer.push(rootScope);
    }

    public ScopeStackTrace(ScopeStackTrace scopeStackTrace, Scope scope) {
        this.scopeStackContainer = new Stack<>();
        for (Scope scopeStackEle: scopeStackTrace.scopeStackContainer) {
            this.scopeStackContainer.push(scopeStackEle);
        }
        this.scopeStackContainer.push(scope);
    }

    public Scope getCurrentScope() {
        return scopeStackContainer.peek();
    }
    public Scope getParentScope() {
        if (scopeStackContainer.size() <= 1) {
            return null;
        }
        return scopeStackContainer.get(scopeStackContainer.size() - 2);
    }

    public String getTrace() {
        StringBuilder sb = new StringBuilder();
        for (int i = 1; i < scopeStackContainer.size(); i++) {
            sb.append(scopeStackContainer.get(i).getIndividualName());
            if (i != scopeStackContainer.size() - 1) {
                sb.append("_");
            }
        }
        return sb.toString();
    }
}
